package my.client.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.client.model.VehHistSearchCriteria;
import my.client.model.Vehicle;
import my.client.service.VehicleHistoryServiceDelegator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.annotation.Secured;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class VehicleHistoryServiceController extends MultiActionController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	private VehicleHistoryServiceDelegator vhServiceDelegator;

	@Secured("ROLE_USER")
	public ModelAndView search(HttpServletRequest request,
			HttpServletResponse response, VehHistSearchCriteria criteria) throws Exception {
		logger.trace("In search");
		ModelAndView mv = new ModelAndView();
		String page = request.getParameter("criteria-page");
		if (page == null) {
			logger.debug("Returning the search page");
			mv.setViewName("vehicle-history-search");
		} else {
			logger.debug("Entered criteria: {}", criteria);
			List<Vehicle> vehicles = vhServiceDelegator
					.getVehicleHistory(criteria);
			logger.debug("Search results {} for criteria {}", vehicles, criteria);
			mv.addObject("vehicles", vehicles);
			mv.setViewName("vehicle-history-search-results");
		}
		return mv;
	}

	@Secured("ROLE_ADMIN")
	public ModelAndView remove(HttpServletRequest request,
			HttpServletResponse response, Vehicle vehicle) throws Exception {
		logger.trace("In remove");
		ModelAndView mv = new ModelAndView("vehicle-history-remove");
		return mv;
	}

	/**
	 * @param vhServiceDelegator
	 */
	public void setVhServiceDelegator(
			VehicleHistoryServiceDelegator vhServiceDelegator) {
		this.vhServiceDelegator = vhServiceDelegator;
	}

}
